<template>
  <div class="home">
    <!-- 轮播图 -->
    <van-swipe :autoplay="3000" indicator-color="white">
      <!-- <van-swipe-item v-for="index in 4" :key="index"> -->
      <van-swipe-item>
        <div class="swipe-item">
          <div class="word">
            <!-- <div class="title color-black-0">垃圾分类小助手</div>
            <div class="content color-black-0 font-w-8">一键查询免烦恼</div> -->
          </div>
        </div>
      </van-swipe-item>
    </van-swipe>
    <div class="search-box-re">
      <div class="search-box">
        <!-- 搜索框 -->
        <div class="flex">
          <div class="address flex font-16 font-w-6" @click="showPicker?showPicker=false:showPicker=true"><span
              style="margin-right:2px">{{address}}</span>
            <van-icon name="arrow-down" />
          </div>
          <div class="search font-18 flex" @click="goOther('/search')">
            <van-icon name="search" /><span class="font-13 color-black-6">请输入正确的名称(包括材质)</span></div>
        </div>
        <!-- 历史记录 -->
        <div class="more-search-box" v-show="list&&list.length>0">
          <div class="font-13 color-black-6">历史记录</div>
          <div class="flex more-search">
            <div class="more-search-item flex font-14 color-black-8" v-for="(item,index) in list" :key="index"
              @click="goOther('/search',item.name,item.type)">
              <div class="text-center title-1">{{item.name}}</div>
              <van-button class="garbage-item" v-if="item.type==1" color="#426BBA" size="mini">可回收垃圾</van-button>
              <van-button class="garbage-item" v-else-if="item.type==2" color="#B83847" size="mini">有害垃圾</van-button>
              <van-button class="garbage-item" v-else-if="item.type==3" color="#18786D" size="mini">易腐垃圾</van-button>
              <van-button class="garbage-item" v-else color="#F9A308" size="mini">其它垃圾</van-button>
            </div>
          </div>
        </div>
        <!-- 热门搜索 -->
        <div class="more-search-box">
          <div class="font-13 color-black-6">热门搜索</div>
          <div class="flex more-search">
            <div class="more-search-item flex font-14 color-black-8" v-for="(item,index) in list2" :key="index"
              @click="goOther('/search',item.name,item.type)">
              <div class="text-center title-1">{{item.name}}</div>
              <van-button class="garbage-item" v-if="item.type==1" color="#426BBA" size="mini">可回收垃圾</van-button>
              <van-button class="garbage-item" v-else-if="item.type==2" color="#B83847" size="mini">有害垃圾</van-button>
              <van-button class="garbage-item" v-else-if="item.type==3" color="#18786D" size="mini">易腐垃圾</van-button>
              <van-button class="garbage-item" v-else color="#F9A308" size="mini">其它垃圾</van-button>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="content-box">
      <div class="text-center font-18 font-w-8">
        <div>垃圾基本分类</div>
        <div class="flex class-content">
          <div class="item font-14 font-w-6" v-for="(i,index) in list3" :key="index+100"
            @click="goOther('/garbageDetail',index+1)">
            <img :src="i.img" alt="">
            <div>{{i.name}}</div>
          </div>
        </div>
      </div>
    </div>
    <div class="foot font-13 color-black-6 text-center">查询结果仅供参考,具体分类要求以当地的政府主管部门为准</div>
    <van-popup v-model="showPicker" position="bottom">
      <van-picker show-toolbar :columns="columns" @cancel="showPicker = false" @confirm="onConfirm" />
    </van-popup>
    <div class="question" @click="goOther('/querstion')">挑战答题</div>
  </div>
</template>

<script>
// @ is an alias to /src
// import HelloWorld from '@/components/HelloWorld.vue'
import { mapState } from "vuex";
import { log } from "util";
export default {
  name: "home",
  data() {
    return {
      citys: `北京，天津，上海，重庆，石家庄，邯郸，太原，呼和浩特，沈阳，大连，长春，哈尔滨，南京，苏州，杭州，宁波，合肥，铜陵，福州，厦门，南昌，宜春，郑州，济南，泰安，青岛，武汉，宜昌，长沙，广州，深圳，南宁，海口，成都，广元，德阳，贵阳，昆明，拉萨，日喀则，西安，咸阳，兰州，西宁，银川，乌鲁木齐`,
      showPicker: false,
      address: "杭州",
      columns: [],
      list2: [
        { name: "塑料袋", type: 4 },
        { name: "报纸", type: 1 },
        { name: "节能灯", type: 2 },
        { name: "面包", type: 3 }
      ],
      list3: [
        { name: "可回收垃圾", img: require("../assets/img/reback.png") },
        { name: "有害垃圾", img: require("../assets/img/danger.png") },
        { name: "易腐垃圾", img: require("../assets/img/3.png") },
        { name: "其它垃圾", img: require("../assets/img/4.png") }
      ]
    };
  },
  computed: {
    ...mapState({
      // 搜索历史记录
      list: state => state.searchHistory
    })
  },
  components: {
    // HelloWorld
  },
  created() {
    this.columns = this.citys.split("，");
  },
  mounted() {},
  methods: {
    /**
     * @description: 地址选择
     * @Author: 罗安兵（605762797@qq.com）
     * @param {String} value 选中值
     */
    onConfirm(value) {
      this.address = value;
      this.showPicker = false;
    },
    /**
     * @description: 页面跳转
     * @Author: 罗安兵（605762797@qq.com）
     * @param {String} url 链接地址
     * @param {String} p1 参数1
     * @param {String} p2 参数2
     */
    goOther(url, p1, p2) {
      this.$router.push({
        path: url,
        query: {
          type: p1,
          title: p2
        }
      });
    }
  }
};
</script>
<style lang="scss" scoped>
.home {
  background-color: #f7f7f7;
  min-height: calc(100vh);
  height: 100%;
  .question {
    color: #fff;
    font-size: 16px;
    line-height: 16px;
    padding: 8px 0 8px 16px;
    height: 14px;
    border-radius: 15px 0 0 15px;
    background-color: #426bba;
    width: 80px;
    position: fixed;
    top: 40px;
    right: 0;
  }
  .text-center {
    text-align: center;
    line-height: 25px;
  }
  .title-1{
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    width: 60px;
  }
  .swipe-item {
    justify-content: space-between;
    background: url("../assets/img/timg3.jpeg") no-repeat;
    background-position-y: -50px;
    background-size: 100%;
    height: 150px;
    .word {
      padding: 20px 0 0 20px;
    }
    .title {
      font-size: 18px;
      font-weight: 800;
      line-height: 30px;
    }
    .content {
      font-size: 14px;
      line-height: 20px;
      font-weight: 400;
    }
    .link {
      font-size: 13px;
      font-weight: 600;
      line-height: 40px;
    }
    .imgs {
      img {
        width: 100px;
      }
    }
  }
  .search-box-re {
    position: relative;
    .search-box {
      // height: 160px;
      padding: 16px 20px;
      margin: -40px 15px 0 15px;
      background-color: #fff;
      box-shadow: 0px 10px 10px 0px rgba(148, 174, 191, 0.3);
      border-radius: 4px;
      .address {
        align-items: center;
        white-space: nowrap;
      }
      .search {
        padding: 0 10px;
        white-space: nowrap;
        align-items: center;
        flex: 1;
        margin-left: 10px;
        background-color: #f2f4f7;
        height: 30px;
        border-radius: 2px;
      }
      .more-search-box {
        margin: 10px 0;
      }
      .more-search {
        margin: 10px 0;
        justify-content: flex-start;
        // flex-wrap: wrap;
        .more-search-item {
          margin-right: 10px;
          flex-direction: column;
          align-items: center;
          justify-content: center;
          padding: 2px 0px;
          border-radius: 3px;
          .garbage-item {
            width: 63px;
          }
        }
      }
    }
  }
  .content-box {
    margin: 20px 15px 20px;
    padding: 30px 10px 20px;
    background-color: #fff;
    box-shadow: 0px 10px 10px 0px rgba(148, 174, 191, 0.3);
    border-radius: 4px;
    min-height: 200px;
    .class-content {
      justify-content: space-around;
      flex-wrap: wrap;
      .item {
        margin-top: 10px;
        padding: 10px 25px;
        border: 1px solid rgba(148, 174, 191, 0.3);
        box-shadow: 0px 4px 4px 0px rgba(148, 174, 191, 0.3);
        border-radius: 6px;
        img {
          height: 70px;
          width: 70px;
        }
      }
    }
  }
  .foot {
    width: 100%;
    height: 60px;
  }
}
</style>
